package com.bobi.tfca.model.pojo;

import cn.hutool.http.HttpStatus;

/**
 * 统一响应体
 */
public record R<T>(int code, String msg, T data) {

    private static final int SUCCESS_CODE = HttpStatus.HTTP_OK;

    private static final int ERROR_CODE = HttpStatus.HTTP_INTERNAL_ERROR;

    private static final int FAILED_CODE = 600;

    private static <T> R<T> success(String msg, T data) {
        return new R<>(SUCCESS_CODE, msg, data);
    }

    public static <T> R<T> success() {
        return success(null, null);
    }

    public static <T> R<T> success(String msg) {
        return success(msg, null);
    }

    public static <T> R<T> data(T data) {
        return success(null, data);
    }

    public static <T> R<T> error(String msg) {
        return new R<>(ERROR_CODE, msg, null);
    }

    //Jwt过期
    public static <T> R<T> JwtExpiredError(String msg) {
        return new R<>(410, msg, null);
    }

    //Jwt篡改
    public static <T> R<T> JwtSignatureError(String msg) {
        return new R<>(411, msg, null);
    }

    //无Jwt，未登录
    public static <T> R<T> NoLoginError(String msg) {
        return new R<>(412, msg, null);
    }

    //无权限
    public static <T> R<T> NoAuthorityError(String msg) {
        return new R<>(413, msg, null);
    }

    public static <T> R<T> failed(String status) {
        return new R<>(FAILED_CODE, status, null);
    }

    public R<T> setData(T newData) {
        return new R<>(this.code(), this.msg(), newData);
    }
}